package org.urchr.beorganized.mobile.pages.secured.starred;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.urchr.beorganized.mobile.base.components.link.MobileLink;
import org.urchr.beorganized.mobile.base.network.NetworkException;
import org.urchr.beorganized.mobile.base.network.XMLRequest;
import org.urchr.beorganized.mobile.pages.secured.base.SecuredPage;
import org.urchr.beorganized.mobile.pages.secured.browse.BrowsePage;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class StarredPage extends SecuredPage {

	public StarredPage(PageParameters parameters) {
		super(parameters);
		final int noteID = parameters.getAsInteger("id", -1);
		add(new MobileLink("back", "action/top.png", 
				new BookmarkablePageLink("clink", BrowsePage.class, 
						new PageParameters("id="+noteID)), "Go back"));
		
		XMLRequest request = new XMLRequest("StarList");
		resetError();
		try {
			Element list = request.callOperation();
			List<Element> l = new ArrayList<Element>();
			NodeList nl = list.getChildNodes();
			for (int i = 0; i < nl.getLength(); i++) {
				if(nl.item(i) instanceof Element)
					l.add((Element) nl.item(i));
			}
			
			ListView<Element> listView = new ListView<Element>("items", l){

				private static final long serialVersionUID = -1724307562755403414L;

				@Override
				protected void populateItem(ListItem<Element> item) {
					Element e = item.getModelObject();
					MobileLink link = new MobileLink("link", "types/p"+e.getAttribute("icon")+".png", 
							e.getAttribute("add_icon"),
							new BookmarkablePageLink("clink", BrowsePage.class, new PageParameters("id="+e.getAttribute("id"))),
							e.getAttribute("text"));
					item.add(link);
				}				
			};
			
			add(listView);
		} catch (NetworkException e) {
			e.printStackTrace();
			showError(e.getErrorString());
			add(new Label("items").setVisible(false));
		}
	}

}
